﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

public partial class _Photos : System.Web.UI.Page 
{
    int iGalleryID;
    int iPictureID;
    protected LocalFunctions FUNC;
    protected string sGalleryTitle;
    protected string sGalleryImages;
    protected void Page_Load(object sender, EventArgs e)
    {
        FUNC = ((Rondo)Master).FUNC;
        GetParameters();
        
        GetGaleryByID();
    }

    private void GetParameters()
    {
        iGalleryID= Convert.ToInt32(Request.QueryString["gId"]);
        if (iGalleryID == 0)
        {
            lnkPhotos.Attributes.Add("style", (FUNC.LangID == 1) ? FUNC.sSelected : FUNC.sSelectedHeb);
            iGalleryID = 2;
            lnkNextGal.Visible = false;
            lnkPrevGal.Visible = false;
        }
        else
        {
            lnkGallery.Attributes.Add("style", (FUNC.LangID == 1) ? FUNC.sSelected : FUNC.sSelectedHeb);
        }
        iPictureID = Convert.ToInt32(Request.QueryString["pId"]);
    }

    private void GetGaleryByID()
    {
        Dal.arParams.Clear();
        Dal.arParams.Add(FUNC.LangID);
        Dal.arParams.Add(iGalleryID);
        Dal.arParams.Add(0);
        DataSet DS = Dal.ExecuteDBspDS("RD_GetGalleryByID", Dal.arParams);
        if (DS != null)
        {
            // Set gallery title
            if (DS.Tables.Count > 0)
            {
                foreach (DataRow DR in DS.Tables[0].Rows)
                {
                    int iTmpID = Convert.ToInt32(DR["id"]);

                    if (iTmpID < iGalleryID)
                    {
                        lnkPrevGal.Text = FUNC.GetPhrase(40);
                        lnkPrevGal.NavigateUrl = "photos.aspx?gid=" + iTmpID;
                    }
                    else
                        if (iTmpID == iGalleryID)
                        {
                            sGalleryTitle = DR["Title"].ToString();
                            Title = sGalleryTitle;
                        }
                        else
                            if (iTmpID > iGalleryID)
                            {
                                lnkNextGal.Text = FUNC.GetPhrase(41);
                                lnkNextGal.NavigateUrl = "photos.aspx?gid=" + iTmpID;
                            }
                }
            }
            // Load gallery images
            if (DS.Tables[1]!=null)
            {
                int iStartFrame = 1;
                for(int iCounter=0;iCounter<DS.Tables[1].Rows.Count;iCounter++)
                {
                    DataRow DR = DS.Tables[1].Rows[iCounter];
                    if (Convert.ToInt32(DR["ID"]) == iPictureID)
                        iStartFrame = (iCounter + 1);
                    string sImage = string.Format("<li><img src=\"gallery/{0}\" /></li>",DR["imagepath"].ToString());
                    sGalleryImages+= sImage;
                }

                string sScript = "<script type=\"text/javascript\">$(document).ready(function() {" +
                    //var iStartFrame = document.getElementById("ctl00_MainContent_hfPid").value;
                    //alert(iStartFrame);
                "$('#gallery').galleryView({" +
                    "panel_width: 730," +
                    "panel_height: 458," +
                    "frame_width: 64," +
                    "frame_height: 64," +
                    "transition_speed: 1200," +
                    "background_color: '#222'," +
                    "border: 'none'," +
                    "easing: 'easeInOutBack'," +
                    "pause_on_hover: true," +
                    "nav_theme: 'custom'," +
                    "start_frame: " +iStartFrame +
                "});});</script>";
                RegisterStartupScript("GalleryScript",sScript);
            }
        }
    }
}
